/**  
* 
* <p>Copyright: Copyright (c) 2017</p>  
* <p>Company: http://www.richfit.com/</p>  
* @author hushouquan
* @date 2020年5月25日  
* @version 1.0  
*/  
package com.rf.richfitwheel.admin.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.ConfigMapper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.admin.sys.model.Config;
import com.rf.richfitwheel.admin.sys.model.ConfigExample;
import com.rf.richfitwheel.admin.sys.model.User;
import com.rf.richfitwheel.admin.sys.service.ConfigService;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;

import net.sf.json.JSONObject;

/**  
*  系统参数管理
* @author hushouquan
* @date 2020年5月25日  
*/
@Service
public class ConfigServiceImpl implements ConfigService {

	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
	@Autowired
   	private ConfigMapper configMapper;
	/* （非 Javadoc）
	 * queryPage
	 * @param params
	 * @return  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#queryPage(java.util.Map)  
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils=PageUtils.getPageInfo(params);
		Page<Config> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		Config config = (Config) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),Config.class);
		ConfigExample example = new ConfigExample();
		ConfigExample.Criteria criteria = example.createCriteria();
		criteria.andStatusNotEqualTo(Constant.COMMON_STATUS_DELETED);
		if(StringUtils.isNotBlank(config.getParamKey())) {
			criteria.andParamKeyLike("%" + config.getParamKey().trim() + "%");
		}
		if(StringUtils.isNotBlank(config.getParamValue())) {
			criteria.andParamValueLike("%" + config.getParamValue().trim() + "%");
		}
		if(StringUtils.isNotBlank(config.getRemark())) {
			criteria.andRemarkLike("%" + config.getRemark().trim() + "%");
		}
		example.setOrderByClause(" param_key ");
		List<Config> list = configMapper.selectByExample(example);
		pageUtils =new PageUtils(p);
		return pageUtils;
	}
	/* （非 Javadoc）
	 * save
	 * @param config  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#save(com.rf.cloud.annuity.sys.model.Config)  
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(Config config, UserVO userVO) {
		boolean hasParamkey = checkHasParamkey(config.getParamKey());
		if(hasParamkey) {
			throw new BusinessException("参数编码已被占用");
		}
		Date date = new Date();
		config.setId(UuidUtil.get32UUID());
		config.setCreator(userVO.getId());
		config.setCreateTime(date);
		config.setModUser(userVO.getId());
		config.setModTime(date);
		configMapper.insert(config);
	}
	
	/**  
	 * 校验paramKey是否重复
	 * @param paramKey
	 * @return  
	 */ 
	public boolean checkHasParamkey(String paramKey) {
		ConfigExample example = new ConfigExample();
		ConfigExample.Criteria criteria = example.createCriteria();
		criteria.andParamKeyEqualTo(paramKey);
		List<Config> list = configMapper.selectByExample(example);
		if(list.size() > 0) {
			return true;
		}
		return false;
	}
	/* （非 Javadoc）
	 * update
	 * @param config  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#update(com.rf.cloud.annuity.sys.model.Config)  
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(Config config, UserVO userVO) {
		Date date = new Date();
		config.setCreator(userVO.getId());
		config.setCreateTime(date);
		config.setModUser(userVO.getId());
		config.setModTime(date);
		configMapper.updateByPrimaryKeySelective(config);
	}
	/* （非 Javadoc）
	 * deleteBatch
	 * @param ids  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#deleteBatch(java.lang.String[])  
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] ids) {
		for(String id:ids){
			configMapper.deleteByPrimaryKey(id);
		}
	}
	/* （非 Javadoc）
	 * selectById
	 * @param id
	 * @return  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#selectById(java.lang.String)  
	 */
	@Override
	public Config selectById(String id) {
		return configMapper.selectByPrimaryKey(id);
	}
	/* （非 Javadoc）
	 * getParamVlueByKey
	 * @param paramKey
	 * @return  
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#getParamVlueByKey(java.lang.String)  
	 */
	@Override
	public Config getParamVlueByKey(String paramKey) {
		ConfigExample example = new ConfigExample();
		ConfigExample.Criteria criteria = example.createCriteria();
		criteria.andParamKeyEqualTo(paramKey);
		criteria.andStatusEqualTo(Constant.COMMON_STATUS_NORMAL);
		List<Config> list = configMapper.selectByExample(example);
		if(list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
